#include "StdAfx.h"
#include "LuaWnd.h"

CLuaEdit::CLuaEdit()
{
}

CLuaEdit::~CLuaEdit()
{

}

bool CLuaEdit::Init( lua_State* L, const char* name )
{
	if (!__super::Init(L, name)) return false;

	lua_getglobal(L, name);

	REG_STATIC_FUNC(L, CLuaEdit, SetWindowText);
	REG_STATIC_FUNC(L, CLuaEdit, GetWindowText);
	REG_STATIC_FUNC(L, CLuaEdit, AppendWindowText);
	
	lua_pop(L, 1);

	return true;
}

CLuaEdit& CLuaEdit::Instance()
{
	static CLuaEdit m_edit;
	return m_edit;
}

CWnd* CLuaEdit::GetWnd( lua_State* L, int idx/*=1*/ )
{
	void* obj = GetObj(L, idx);
	if (obj != NULL)
	{
		CWnd* wnd = (CWnd*)obj;
		if (wnd != NULL && ::IsWindow(wnd->m_hWnd))
		{
			return wnd;
		}
	}

	return NULL;
}

int CLuaEdit::SetWindowText( lua_State* L )
{
	CWnd* wnd = GetWnd(L);
	if (wnd != NULL)
	{
		wnd->SetWindowText(CScript::stringFormat(L, 2).c_str());
	}
	

	return 0;
}

int CLuaEdit::GetWindowText( lua_State* L )
{
	CWnd* wnd = GetWnd(L);
	if (wnd != NULL)
	{
		CString str;
		wnd->GetWindowText(str);
		lua_pushstring(L, str);
		return 1;		
	}
	return 0;
}

int CLuaEdit::AppendWindowText( lua_State* L )
{
	CWnd* wnd = GetWnd(L);
	if (wnd != NULL)
	{
		std::string strMessage =  CScript::stringFormat(L, 2);
		if (!strMessage.empty())
		{
			CString str;
			wnd->GetWindowText(str);
			str = CString(strMessage.c_str() + CString("\r\n")) + str;
			wnd->SetWindowText(str);
		}

	}
	return 0;
}



CLuaListCtrl& CLuaListCtrl::Instance()
{
	static CLuaListCtrl m_list;
	return m_list;
}

CLuaListCtrl::CLuaListCtrl()
{

}

CLuaListCtrl::~CLuaListCtrl()
{

}

bool CLuaListCtrl::Init( lua_State* L, const char* name )
{
	if (!__super::Init(L, name)) return false;

	lua_getglobal(L, name);

	REG_STATIC_FUNC(L, CLuaListCtrl, GetItemCount);
	REG_STATIC_FUNC(L, CLuaListCtrl, GetColumnCount);
	REG_STATIC_FUNC(L, CLuaListCtrl, GetItemText);
	REG_STATIC_FUNC(L, CLuaListCtrl, SetItemText);
	REG_STATIC_FUNC(L, CLuaListCtrl, GetColumn);
	REG_STATIC_FUNC(L, CLuaListCtrl, SetColumn);
	REG_STATIC_FUNC(L, CLuaListCtrl, DeleteAllItems);
	REG_STATIC_FUNC(L, CLuaListCtrl, DeleteAllColumns);
	REG_STATIC_FUNC(L, CLuaListCtrl, DeleteColumn);
	REG_STATIC_FUNC(L, CLuaListCtrl, DeleteItem);
	REG_STATIC_FUNC(L, CLuaListCtrl, InsertColumn);
	REG_STATIC_FUNC(L, CLuaListCtrl, InsertItem);
	

	lua_pop(L, 1);

	return true;
}

CWnd* CLuaListCtrl::GetWnd( lua_State* L, int idx/*=1*/ )
{
	void* obj = GetObj(L, idx);
	if (obj != NULL)
	{
		CWnd* wnd = (CWnd*)obj;
		if (wnd != NULL && ::IsWindow(wnd->m_hWnd))
		{
			return wnd;
		}
	}

	return NULL;
}



int CLuaListCtrl::GetItemCount( lua_State* L )
{
	CWnd* wnd = GetWnd(L);
	if (wnd != NULL)
	{
		CListCtrl& listCtrl = (CListCtrl&)*wnd;
		lua_pushinteger(L, listCtrl.GetItemCount());
		return 1;
	}
	return 0;
}

/*
CString GetItemText(
int nItem,
int nSubItem 
) const

*/
int CLuaListCtrl::GetItemText( lua_State* L )
{
	CWnd* wnd = GetWnd(L);
	if (wnd != NULL)
	{
		CListCtrl& listCtrl = (CListCtrl&)*wnd;
		int nItem = lua_tointeger(L, 2);
		int nSubItem = lua_tointeger(L, 3);
		CString str = listCtrl.GetItemText(nItem, nSubItem);
		lua_pushstring(L, str);
		return 1;
	}
	return 0;
}

int CLuaListCtrl::SetItemText( lua_State* L )
{
	CWnd* wnd = GetWnd(L);
	if (wnd != NULL)
	{
		CListCtrl& listCtrl = (CListCtrl&)*wnd;
		int nItem = lua_tointeger(L, 2);
		int nSubItem = lua_tointeger(L, 3);
		const char* szText = lua_tostring(L, 4);
		if (szText != NULL)
		{
			listCtrl.SetItemText(nItem, nSubItem, szText);
			lua_pushboolean(L, true);
		}
		else
			lua_pushboolean(L, false);
		return 1;
	}
	return 0;
}

int CLuaListCtrl::GetColumnCount( lua_State* L )
{
	CWnd* wnd = GetWnd(L);
	if (wnd != NULL)
	{
		CListCtrl& listCtrl = (CListCtrl&)*wnd;
		int nColumnCount = listCtrl.GetHeaderCtrl()->GetItemCount();
		lua_pushinteger(L, nColumnCount);
		return 1;
	}
	return 0;
}



#define SET_ICOLUMN(L, col, name)\
	lua_pushinteger(L, col.name);\
	lua_setfield(L, -2, #name);

/*
int nCol,
LVCOLUMN* pColumn 
*/
int CLuaListCtrl::GetColumn( lua_State* L )
{
	CWnd* wnd = GetWnd(L);
	if (wnd != NULL)
	{
		CListCtrl& listCtrl = (CListCtrl&)*wnd;
		int nCol = lua_tointeger(L, 2);
		UINT mask = (UINT)lua_tointeger(L, 3);

		/*
		typedef struct _LVCOLUMN { 
		UINT mask; 
		int fmt; 
		int cx; 
		LPTSTR pszText; 
		int cchTextMax; 
		int iSubItem; 
		#if (_WIN32_IE >= 0x0300)
		int iImage;
		int iOrder;
		#endif
		#if (_WIN32_WINNT >= 0x0600)
		int cxMin;
		int cxDefault;
		int cxIdeal;
		#endif
		*/
		LVCOLUMN col;
		col.mask = mask;
		if (listCtrl.GetColumn(0, &col))
		{
			lua_newtable(L);

			SET_ICOLUMN(L, col, mask);
			SET_ICOLUMN(L, col, fmt);
			SET_ICOLUMN(L, col, cx);
			SET_ICOLUMN(L, col, cchTextMax);
			SET_ICOLUMN(L, col, iSubItem);
			SET_ICOLUMN(L, col, iImage);
			SET_ICOLUMN(L, col, iOrder);
			SET_ICOLUMN(L, col, cxMin);
			SET_ICOLUMN(L, col, cxDefault);
			SET_ICOLUMN(L, col, cxIdeal);

			lua_pushstring(L, col.pszText);\
			lua_setfield(L, -2, "pszText");
			return 1;
		}

	}
	return 0;
}

#define GET_ICOLUMN(L, col, name)\
	lua_pushstring(L, #name);\
	lua_rawget(L, -2);\
	col.name = lua_tointeger(L, -1);\
	lua_pop(L, 1);

int CLuaListCtrl::SetColumn( lua_State* L )
{
	CWnd* wnd = GetWnd(L);
	if (wnd != NULL)
	{
		CListCtrl& listCtrl = (CListCtrl&)*wnd;
		/*
		typedef struct _LVCOLUMN { 
		UINT mask; 
		int fmt; 
		int cx; 
		LPTSTR pszText; 
		int cchTextMax; 
		int iSubItem; 
		#if (_WIN32_IE >= 0x0300)
		int iImage;
		int iOrder;
		#endif
		#if (_WIN32_WINNT >= 0x0600)
		int cxMin;
		int cxDefault;
		int cxIdeal;
		#endif
		*/
		int nCol = lua_tointeger(L, -2);
		LVCOLUMN col;
		GET_ICOLUMN(L, col, mask);
		GET_ICOLUMN(L, col, fmt);
		GET_ICOLUMN(L, col, cx);
		GET_ICOLUMN(L, col, cchTextMax);
		GET_ICOLUMN(L, col, iSubItem);
		GET_ICOLUMN(L, col, iImage);
		GET_ICOLUMN(L, col, iOrder);
		GET_ICOLUMN(L, col, cxMin);
		GET_ICOLUMN(L, col, cxDefault);
		GET_ICOLUMN(L, col, cxIdeal);

		lua_pushstring(L, "pszText");
		lua_rawget(L, -2);
		col.pszText = (LPSTR)lua_tostring(L, -1);
		lua_pop(L, 1);
		
		if (listCtrl.SetColumn(nCol, &col))
			lua_pushboolean(L, true);
		else
			lua_pushboolean(L, false);
		return 1;
		

	}
	return 0;
}

int CLuaListCtrl::DeleteAllItems( lua_State* L )
{
	CWnd* wnd = GetWnd(L);
	if (wnd != NULL)
	{
		CListCtrl& listCtrl = (CListCtrl&)*wnd;
		if (listCtrl.DeleteAllItems())
			lua_pushboolean(L, true);
		else
			lua_pushboolean(L, false);
		return 1;
	}
	return 0;
}

int CLuaListCtrl::DeleteColumn( lua_State* L )
{
	CWnd* wnd = GetWnd(L);
	if (wnd != NULL)
	{
		CListCtrl& listCtrl = (CListCtrl&)*wnd;
		int nCol = lua_tointeger(L, -1);
		if (listCtrl.DeleteColumn(nCol))
			lua_pushboolean(L, true);
		else
			lua_pushboolean(L, false);
		return 1;
	}
	return 0;
}

int CLuaListCtrl::DeleteAllColumns( lua_State* L )
{
	CWnd* wnd = GetWnd(L);
	if (wnd != NULL)
	{
		CListCtrl& listCtrl = (CListCtrl&)*wnd;
		int nColumnCount = listCtrl.GetHeaderCtrl()->GetItemCount();

		for (int i=0; i < nColumnCount; i++)
		{
			listCtrl.DeleteColumn(0);
		}		
	}
	return 0;
}


int CLuaListCtrl::DeleteItem( lua_State* L )
{
	CWnd* wnd = GetWnd(L);
	if (wnd != NULL)
	{
		CListCtrl& listCtrl = (CListCtrl&)*wnd;
		int nItem = lua_tointeger(L, -1);
		if (listCtrl.DeleteItem(nItem))
			lua_pushboolean(L, true);
		else
			lua_pushboolean(L, false);
		return 1;
	}
	return 0;
}

/*
int InsertColumn(
int nCol,
LPCTSTR lpszColumnHeading,
int nFormat = LVCFMT_LEFT,
int nWidth = -1,
int nSubItem = -1 
);
*/
int CLuaListCtrl::InsertColumn( lua_State* L )
{
	CWnd* wnd = GetWnd(L);
	if (wnd != NULL)
	{
		CListCtrl& listCtrl = (CListCtrl&)*wnd;
		int len = lua_gettop(L);
		int nCol = lua_tointeger(L, 2);
		LPCTSTR lpszColumnHeading = lua_tostring(L, 3);
		int nFormat = LVCFMT_LEFT;
		int nWidth = -1;
		int nSubItem = -1;
		if (len > 3) nFormat = lua_tointeger(L, 4);
		if (len > 4) nWidth = lua_tointeger(L, 5);
		if (len > 5) nSubItem = lua_tointeger(L, 6);

		if (lpszColumnHeading != NULL)
		{
			int ret = listCtrl.InsertColumn(nCol, lpszColumnHeading, nFormat, nWidth, nSubItem);
			lua_pushinteger(L, ret);
			return 1;
		}
	}
	return 0;
}

/*
int InsertItem(
int nItem,
LPCTSTR lpszItem 
);

*/
int CLuaListCtrl::InsertItem( lua_State* L )
{
	CWnd* wnd = GetWnd(L);
	if (wnd != NULL)
	{
		int ret = 0;
		CListCtrl& listCtrl = (CListCtrl&)*wnd;
		int nItem = lua_tointeger(L, 2);
		if (nItem == -1)
		{
			nItem = listCtrl.GetItemCount();
		}
		LPCTSTR lpszItem = lua_tostring(L, 3);
		if (lpszItem != NULL)
		{
			ret = listCtrl.InsertItem(nItem, lpszItem);
		}
		int len = lua_gettop(L);
		int iSub = 1;
		for (int i = 4; i <= len; i++)
		{
			const char* szText = lua_tostring(L, i);
			if (szText != NULL)
			{
				listCtrl.SetItemText(nItem, iSub++, szText);
			}
		}
		lua_pushinteger(L, ret);
		return 1;

	}
	return 0;
}



